﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using Web.SystemConfig.SysCache;

namespace Web.SystemConfig.WorkFlow
{
    public class WorkFlowMessageStatusRuleListCache
    {
        private static WorkFlowMessageStatusRuleListCache m_WorkFlowMessageStatusList = null;
        private List<WorkFlowMessageStatusRule> m_ArrayList = null;

        internal WorkFlowMessageStatusRuleListCache()
        {
            if (this.m_ArrayList == null)
            {
                this.m_ArrayList = new List<WorkFlowMessageStatusRule>();
            }
        }

        internal WorkFlowMessageStatusRuleListCache GetInstance()
        {
            if (m_WorkFlowMessageStatusList == null)
            {
                m_WorkFlowMessageStatusList = new WorkFlowMessageStatusRuleListCache();
            }

            ///初始化消息状态信息
            DataTable dt = null;
            WorkFlowMessageStatusRule rule = null;
            string strMsg = new WorkFlowMessageStatusDAL().GetAllWorkFlowOperateRule(out dt);
            if (string.IsNullOrEmpty(strMsg))
            {
                foreach (DataRow dr in dt.Rows)
                {
                    rule = new WorkFlowMessageStatusRule();
                    rule.FromMessageStatusName = dr["FromStatusName"].ToString();
                    rule.FromMessageStatusID = int.Parse(dr["FromStatusID"].ToString());
                    rule.OperateID = int.Parse(dr["OperateID"].ToString());
                    rule.OperateName = dr["OperateName"].ToString();
                    rule.TableID = int.Parse(dr["TableID"].ToString());
                    rule.ToMessageStatusID = int.Parse(dr["ToStatusID"].ToString());
                    rule.ToMessageStatusName = dr["ToStatusName"].ToString();
                    rule.ToRoleID = int.Parse(dr["ToRoleID"].ToString());
                    rule.FromRoleID = int.Parse(dr["FromRoleID"].ToString());
                    this.m_ArrayList.Add(rule);
                }
            }
            return m_WorkFlowMessageStatusList;
        }

        /// <summary>
        /// 获取某业务表所有驱动规则
        /// </summary>
        /// <param name="tableID"></param>
        /// <returns></returns>
        public List<WorkFlowMessageStatusRule> GetAllRules(int tableID)
        {
            List<WorkFlowMessageStatusRule> rtnRuleList = new List<WorkFlowMessageStatusRule>();
            foreach (WorkFlowMessageStatusRule rule in this.m_ArrayList)
            {
                if (rule.TableID == tableID)
                {
                    rtnRuleList.Add(rule);
                }
            }
            return rtnRuleList;
        }

        /// <summary>
        /// 通过OperateID获取操作规则
        /// </summary>
        /// <param name="operateID"></param>
        /// <returns></returns>
        public WorkFlowMessageStatusRule GetRuleByOperateID(int operateID)
        {
            WorkFlowMessageStatusRule rtnWorkFlowMessageStatusRule = null;
            foreach (WorkFlowMessageStatusRule rule in this.m_ArrayList)
            {
                if (rule.OperateID == operateID)
                {
                    rtnWorkFlowMessageStatusRule = rule;
                    break;
                }
            }
            return rtnWorkFlowMessageStatusRule;
        }

        /// <summary>
        /// 通过当前状态、角色获取该数据下一步的操作
        /// </summary>
        /// <param name="statusId"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<WorkFlowMessageStatusRule> GetNextRulesByStatusIDAndRoleID(int statusId, int roleId)
        {
            var rtnRuleList = new List<WorkFlowMessageStatusRule>();
            this.m_ArrayList.ForEach(delegate(WorkFlowMessageStatusRule rule)
            {
                if (rule.FromMessageStatusID == statusId && rule.FromRoleID == roleId)
                {
                    rtnRuleList.Add(rule);
                }
            });

            return rtnRuleList;
        }

        ///// <summary>
        ///// 获取某业务表初始驱动规则
        ///// </summary>
        ///// <param name="tableID"></param>
        ///// <returns></returns>
        //public WorkFlowMessageStatusRule GetOriginalRule(int tableID)
        //{
        //    WorkFlowMessageStatusRule rtnRule = new WorkFlowMessageStatusRule();
        //    WorkFlowMessageStatus originalMessageStatus = new WorkFlowMessageStatusCache().GetOrginalMessageStatus(tableID);
        //    if (originalMessageStatus == null)
        //    {
        //        throw new Exception("获取缓存配置失败");
        //    }

        //    foreach (WorkFlowMessageStatusRule rule in this.m_ArrayList)
        //    {
        //        if (rule.FromMessageStatusID == originalMessageStatus.StatusID)
        //        {
        //            rtnRule = rule;
        //            break;
        //        }
        //    }

        //    return new WorkFlowMessageStatusRule();
        //}

        /// <summary>
        /// 获取某状态，某角色的驱动规则
        /// </summary>
        /// <param name="statusID"></param>
        /// <returns></returns>
        public List<WorkFlowMessageStatusRule> GetRuleByStatusID(int statusID, int fromRoleID)
        {
            List<WorkFlowMessageStatusRule> rtnRuleList = new List<WorkFlowMessageStatusRule>();
            foreach (WorkFlowMessageStatusRule rule in this.m_ArrayList)
            {
                if (rule.FromMessageStatusID == statusID && rule.FromRoleID == fromRoleID)
                {
                    rtnRuleList.Add(rule);
                    break;
                }
            }

            return rtnRuleList;
        }

        /// <summary>
        /// 获取流程的最终状态，用来标示数据最终入库
        /// </summary>
        /// <param name="tableID"></param>
        /// <returns></returns>
        public int GetFinishStatus(int tableID)
        {
            List<WorkFlowMessageStatusRule> ruleList = this.GetAllRules(tableID);
            int rtnStatusID = 0;
            if (ruleList != null && ruleList.Count > 0)
            {
                foreach (WorkFlowMessageStatusRule rule in ruleList)
                {
                    List<WorkFlowMessageStatusRule> ruleListTemp = this.GetRuleByStatusID(rule.ToMessageStatusID);
                    if (ruleListTemp != null && ruleListTemp.Count > 0)
                    {
                        continue;
                    }
                    else
                    {
                        rtnStatusID = rule.ToMessageStatusID;
                        break;
                    }
                }
            }
            else
            {
                throw new Exception("该业务表无流程最终状态，请重新核查流程配置");
            }
            return rtnStatusID;
        }

        /// <summary>
        /// 获取某状态下某名称的操作规则
        /// </summary>
        /// <param name="stausID"></param>
        /// <param name="operateName"></param>
        /// <returns></returns>
        public WorkFlowMessageStatusRule GetRuleByStatusIDAndOperateName(int stausID, string operateName)
        {
            WorkFlowMessageStatusRule rtnWorkFlowMessageStatusRule = null;
            foreach (WorkFlowMessageStatusRule rule in this.m_ArrayList)
            {
                if (rule.FromMessageStatusID == stausID && rule.OperateName.Trim().ToUpper() == operateName.Trim().ToUpper())
                {
                    rtnWorkFlowMessageStatusRule = rule;
                    break;
                }
            }
            return rtnWorkFlowMessageStatusRule;
        }

        /// <summary>
        /// 获取某状态下驱动规则
        /// </summary>
        /// <param name="statusID"></param>
        /// <returns></returns>
        public List<WorkFlowMessageStatusRule> GetRuleByStatusID(int statusID)
        {
            List<WorkFlowMessageStatusRule> rtnRuleList = new List<WorkFlowMessageStatusRule>();
            foreach (WorkFlowMessageStatusRule rule in this.m_ArrayList)
            {
                if (rule.FromMessageStatusID == statusID)
                {
                    rtnRuleList.Add(rule);
                    break;
                }
            }

            return rtnRuleList;
        }

        /// <summary>
        /// 获取业务表最终消息状态
        /// </summary>
        /// <param name="tableEnum"></param>
        /// <returns></returns>
        public WorkFlowMessageStatus GetFinnalMessageStatusID(ArchiveTableEnum tableEnum)
        {
            WorkFlowMessageStatus rtnWorkFlowMessageStatus = null;
            List<WorkFlowMessageStatus> Statuslist = new ArchiveCacheFacade().GetStausCache().GetAllMessageStatus((int)tableEnum);
            if (Statuslist != null && Statuslist.Count > 0)
            {
                foreach (WorkFlowMessageStatus status in Statuslist)
                {
                    List<WorkFlowMessageStatusRule> tempList = this.GetRuleByStatusID(status.StatusID);
                    if (tempList == null || tempList.Count == 0)
                    {
                        rtnWorkFlowMessageStatus = status;
                        break;
                    }
                }
            }
            return rtnWorkFlowMessageStatus;
        }
    }
}